﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UdpMulticastSender
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // 组播地址
            IPAddress multicastAddress = IPAddress.Parse("239.0.0.1");
            int port = 12345;

            // 创建UdpClient
            using (UdpClient udpClient = new UdpClient())
            {
                // 设置组播TTL为32，可以跨路由
                udpClient.JoinMulticastGroup(multicastAddress, 32);

                while (true)
                {
                    var message = $"当前时间（{DateTime.Now.ToString()}）";
                    byte[] buffer = Encoding.UTF8.GetBytes(message);
                    udpClient.Send(buffer, buffer.Length, new IPEndPoint(multicastAddress, port));
                    Console.WriteLine("发送数据: " + message);
                    Thread.Sleep(1000);
                }
            }
        }
    }
}